// 在Order类中添加
class Order {
    private static int nextId = 1; // 静态变量，用于生成唯一ID

    private int orderId;
    private Customer customer;
    private Coffee coffee;
    private int quantity;
    private String status;
    private double totalPrice; // 添加总价属性

    public Order(Customer customer, Coffee coffee, int quantity) {
        this.orderId = nextId++;
        this.customer = customer;
        this.coffee = coffee;
        this.quantity = quantity;
        this.status = "已下单";
        this.totalPrice = coffee.getPrice() * quantity; // 初始化总价
    }

    // 处理订单的方法（无参数版本）
    public boolean processOrder() {
        double total = coffee.getPrice() * quantity;

        if (customer.makePayment(total)) {
            this.status = "已完成";
            System.out.println("订单#" + orderId + " 处理成功!");

            // 顾客饮用咖啡
            for (int i = 0; i < quantity; i++) {
                customer.drinkCoffee(coffee);
            }

            return true;
        } else {
            this.status = "支付失败";
            return false;
        }
    }
    
    // 处理订单的方法（接受Payment参数的重载版本）
    public boolean processOrder(Payment payment) {
        double total = coffee.getPrice() * quantity;
        
        if (payment.pay(total)) {
            this.status = "已完成";
            System.out.println("订单#" + orderId + " 处理成功!");

            // 顾客饮用咖啡
            for (int i = 0; i < quantity; i++) {
                customer.drinkCoffee(coffee);
            }

            return true;
        } else {
            this.status = "支付失败";
            return false;
        }
    }

    // 显示订单详情
    public void displayOrder() {
        System.out.println("订单号: #" + orderId);
        System.out.println("顾客: " + customer.getName());
        System.out.println("咖啡: " + coffee.getName() + " × " + quantity);
        System.out.println("总价: ¥" + (coffee.getPrice() * quantity));
        System.out.println("状态: " + status);
    }
    
    // 获取顾客
    public Customer getCustomer() {
        return customer;
    }

    // 转换为文件字符串
    public String toFileString() {
        return orderId + "," + customer.getName() + "," + coffee.getName() + "," +
                quantity + "," + totalPrice + "," + status;
    }

    // 从文件字符串创建订单
    public static Order fromFileString(String data) {
        String[] parts = data.split(",");
        if (parts.length != 6) {
            throw new IllegalArgumentException("无效的订单数据格式");
        }

        // 这里需要CoffeeShop实例来获取咖啡和顾客对象
        // 简化实现：创建新对象
        Customer customer = new Customer(parts[1], 100.0); // 默认余额
        Coffee coffee = new Coffee(parts[2], 0, "", 0); // 简化

        Order order = new Order(customer, coffee, Integer.parseInt(parts[3]));
        order.orderId = Integer.parseInt(parts[0]);
        order.totalPrice = Double.parseDouble(parts[4]);
        order.status = parts[5];

        return order;
    }
}